/**
 * @author yzhou2
 */
var QuoteSettingPanel=function(container,callback){
    //this.quoteObj=quoteObj;
    this.container=container;
    this.callback=callback||{};
    this.el=null;
    this.sltAllBtnEl=null;
    this.dataPointChk=null;
    this.scrollbar=null;
    this.width=null;
    this.height=null;
    //this.dataPointList=null;
    this.html='<div class="rtq-quotes-view"><h3>Current View</h3><div class="bd"><div class="datarow"></div></div><div class="ft"><div class="rtq-quote-operation"><a href="javascript:void(0)" class="rtq-checkbox sltall">Select All</a></div></div></div>';
    //this.init();
};
QuoteSettingPanel.prototype={
    init:function(data,selectData){
       this.el=$(this.html);
       if(this.container){
           this.el.appendTo(this.container)
       };
       this.sltAllBtnEl = this.el.find('.sltall');
       this._createDataPointChk(data);  
       this._initEvent();
	   if (data.length == selectData.length) {
	   	   this._selectAll();
	       this.selectAllChkBtn(true);
	   }else{
	   	   this._inverseAll();
	   	   for(var i=0,l=selectData.length;i<l;i++){
		   	   this.dataPointChk.btns[selectData[i].val].select();
		   }
	   }
       this._initScrollBar();
       //this.resize(this.container.width(),this.container.height());
    },
    getDom:function(){
       return this.el[0];
    },
    rebindDataPointChk:function(data,isSelectAll){
        this.dataPointChk.bindData(data);
        this._selectAll();
        this.selectAllChkBtn(isSelectAll);
        this.resize();
    },
    _initScrollBar:function(){
        this.scrollbar=new Util.ScrollPanel(this.el.find('.datarow'), {}); 
    },
    resize:function(w,h){
        var flag=false;
        w=w?w:this.width;
        h=h?h:this.height;
        if(w!=this.width){
            this.width=w;
            flag=true;
        }
        if(h!=this.height){
            this.height=h;
            flag=true;
        }
        if (flag) {
            this.scrollbar.reSize(this.width, this.height);
        }
    },
    _initEvent:function(){
        var self=this;
       this.sltAllBtnEl.bind('click.QuoteSetting',function(e){
            var target=$(e.target);
            var isSelected=false;
            if(target.hasClass('rtq-checkbox-on')){
                target.removeClass('rtq-checkbox-on');
                self._inverseAll();
            }else{
                target.addClass('rtq-checkbox-on');
                self._selectAll();
                isSelected=true;        
            }
            if(typeof self.callback.onClickAll=='function'){
                self.callback.onClickAll(isSelected);
            }
       });
    },
    _inverseAll:function(){
        for(var v in this.dataPointChk.btns){
            this.dataPointChk.btns[v].inverse();
        }
    },
    _selectAll:function(){
        for(var v in this.dataPointChk.btns){
            this.dataPointChk.btns[v].select();
        }
    },
    _createDataPointChk:function(data){
        var self=this;
        this.dataPointChk=new Util.ButtonGroup(this.el.find('.datarow'),data, {
            onClick: function(v, item){                
                if(item.isSelected){
                    if(self._isSelectAll()){
                        self.sltAllBtnEl.addClass('rtq-checkbox-on');
                    }  
                }else{
                    self.sltAllBtnEl.removeClass('rtq-checkbox-on');
                }
                if(typeof self.callback.onClick=='function'){
                    self.callback.onClick(v,item.isSelected);
                }
            }
        },{type:'checkbox',vertical:true});
    },
    _isSelectAll:function(){
        var ret=true;
        for(var v in this.dataPointChk.btns){
            if(!this.dataPointChk.btns[v].el.hasClass('rtq-checkbox-on')){
                ret=false;
                break;
            }
        }
        return ret;
    },
    selectDataPointChk:function(vals,isSelect){
        if(this.dataPointChk==null){
            return;
        }
        isSelect=isSelect===false?false:true;
        for(var i=0,l=vals.length;i<l;i++){
            if (isSelect) {
                this.dataPointChk.btns[vals[i]].select();
            }else{
                this.dataPointChk.btns[vals[i]].inverse();
            }
        }
    },
    selectAllChkBtn:function(isSelect){
        if(isSelect){
            this.sltAllBtnEl.addClass('rtq-checkbox-on');
        }else{
            this.sltAllBtnEl.removeClass('rtq-checkbox-on');
        }
    }
};
